package com.example.logdemo;

import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * @author cxx
 * @create 2021-02-25 10:33
 **/
@Component
public class FirstFiler extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String rid = request.getParameter ("rid");
        if(rid == null || "".equals (rid)){
            rid =  UUID.randomUUID ().toString ().replace ("-","");
        }
        MDC.put ("rid",rid);
        filterChain.doFilter (request,response);
    }
}
